<template>
  <div class="in">
    <div class="in-info">
      <div class="tab-list flex-a">
        <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
          @click="changeTab(item.value)">
          <span>{{ item.name }}</span>
          <i></i>
        </div>
      </div>
      <div class="box">
        <!-- 按入库订单 -->
        <div v-if="tab == 'inOrder'">
          <div class="flex-b">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" class="input">
              <el-form-item>
                <el-date-picker v-model="queryParams.time" style="width:340px" type="datetimerange"
                  :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                  align="right">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-select v-model="queryParams.sub" placeholder="请选择出库类型">
                  <el-option v-for="item in stockTypeList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <!-- <el-form-item>
                <el-select v-model="queryParams.sub" placeholder="请选择操作人">
                  <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item> -->
              <el-form-item>
                <el-input v-model="queryParams.name" prefix-icon="el-icon-search" style="width:340px"
                  placeholder="请输入商品名称/条形码/关联单号/备注/品牌" clearable @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
                <el-button type="warning" class="btn" size="mini" @click="inbound"
                  v-hasPermi="['pet:petInfo:export']">手动入库</el-button>
                <el-button type="warning" class="btn" size="mini" @click="handleExport"
                  v-hasPermi="['pet:petInfo:export']">导出</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="stock-total">入库总金额：<span>{{ assMoney }}</span> 元入库总数：<span>{{ assNum }}</span></div>
          <div>
            <el-table v-loading="loading" :data="pchList">
              <el-table-column label="入库单号" align="center" prop="pchNo" />
              <el-table-column label="入库类型" align="center" prop="pchType" />
              <el-table-column label="入库总数量" align="center" prop="pchNum" />
              <el-table-column label="入库总计" align="center" prop="pchType" />
              <el-table-column label="操作人" align="center" prop="operUser" />
              <el-table-column label="入库时间" align="center" prop="pchNum" />
              <el-table-column label="备注" align="center" prop="pchRemark" />
              <el-table-column label="关联单号" align="center" prop="pchRemark" />
              <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                    v-hasPermi="['pet:pch:edit']">详情</el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                    v-hasPermi="['pet:pch:remove']">撤销</el-button>
                </template>
              </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize" @pagination="getList" />
          </div>
        </div>
        <!-- 按入库商品 -->
        <div v-if="tab == 'inGoods'">
          <div class="flex-b">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" class="input"
              label-width="68px">
              <el-form-item>
                <el-date-picker v-model="queryParams.time" value-format="yyyy-MM-dd" style="width:340px"
                  type="datetimerange" :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期"
                  end-placeholder="结束日期" align="right">
                </el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-select v-model="queryParams.topCate" @change="changeClass" placeholder="请选择分类">
                  <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id + ''">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-select v-model="queryParams.sedCate" placeholder="请选择子分类">
                  <el-option v-for="item in subClassList" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-select v-model="queryParams.sub" clearable placeholder="请选择入库类型">
                  <el-option v-for="item in stockTypeList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <!-- <el-form-item>
                <el-select v-model="queryParams.sub" placeholder="操作人">
                  <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item> -->
              <el-form-item>
                <el-input v-model="queryParams.name" prefix-icon="el-icon-search" style="width:340px"
                  placeholder="请输入关联单号/商品名称/品牌名称/条形码/备注" clearable @keyup.enter.native="handleQuery" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
                <el-button type="warning" class="btn" size="mini" @click="inbound"
                  v-hasPermi="['pet:in:add']">手动入库</el-button>
                <!-- <el-button type="warning" class="btn" size="mini" @click="handleExport"
                  v-hasPermi="['pet:petInfo:export']">导出</el-button> -->
              </el-form-item>
            </el-form>
          </div>
          <div class="stock-total">入库总金额：<span>{{ assMoney }}</span> 元 <span style="padding: 0 5px;"></span> 入库总数：<span>{{ assNum }}</span></div>
          <div>
            <el-table v-loading="loading" :data="pchList">
              <el-table-column label="商品图片" align="center" prop="url">
                <template slot-scope="scope">
                  <img :src="scope.row.url" style="width: 50%;" alt="">
                </template>
              </el-table-column>
              <el-table-column label="商品名称" align="center" prop="name" />
              <el-table-column label="条形码" align="center" prop="no" />
              <el-table-column label="入库类型" align="center" prop="pchType">
                <template slot-scope="scope">
                  {{ scope.row.pchType == 0 ? '采购入库' : '其他入库' }}
                </template>
              </el-table-column>
              <el-table-column label="入库单价" align="center" prop="price" />
              <el-table-column label="入库数量" align="center" prop="num" />
              <el-table-column label="入库小计" align="center" prop="addNum" />
              <!-- <el-table-column label="入库后数量" align="center" prop="outRemark" /> -->
              <el-table-column label="操作人" align="center" prop="operUser" />
              <el-table-column label="入库时间" align="center" prop="timeSting" />
              <el-table-column label="关联单号" align="center" prop="relNo" />
              <el-table-column label="备注" align="center" prop="remark" />
              <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                    v-hasPermi="['pet:out:edit']">详情</el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                    v-hasPermi="['pet:out:remove']">撤销</el-button>
                </template>
              </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize" @pagination="getList" />
          </div>
        </div>
      </div>
    </div>
    <!-- 详情对话框 -->
    <el-dialog title="采购入库详情" :visible.sync="open" width="1000px" append-to-body>
      <div class="in-detail">
        <div>入库单号：20230201101852845918</div>
        <div>入库类型：采购入库</div>
        <div>操作人：宠小家</div>
        <div>入库时间：2023-02-01 10:18:52</div>
        <div>入库备注：初始库存</div>
      </div>
      <el-table v-loading="loading" :data="pchDetailList">
        <el-table-column label="商品图片" align="center" prop="url">
          <template slot-scope="scope">
            <img :src="scope.row.url" alt="">
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="name" />
        <el-table-column label="条形码" align="center" prop="no" />
        <el-table-column label="规格编号" align="center" prop="pchType" />
        <el-table-column label="零售价" align="center" prop="pchPice" />
        <el-table-column label="标准进货价" align="center" prop="pchNum" />
        <el-table-column label="入库单价" align="center" prop="addNum" />
        <el-table-column label="入库数量" align="center" prop="outRemark" />
        <el-table-column label="入库小计" align="center" prop="operUser" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listPch, getPch, delPch, addPch, updatePch } from "@/api/pet/pch";
import { getClassList } from "@/api/pet/class"
export default {
  name: "Pch",
  data() {
    return {
      tab: 'inGoods',
      pchDetailList: [],
      assNum:0,
      assMoney:0,
      tabList: [
        // {
        //   name: '按入库订单',
        //   value: 'inOrder'
        // },
        {
          name: '按入库商品',
          value: 'inGoods'
        },
      ],
      stockTypeList: [
        {
          value: 0,
          label: '采购入库'
        },
        {
          value: 1,
          label: '其他入库'
        },
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      subClassList: [],
      classList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品进货单表格数据
      pchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prodNo: null,
        pchNo: null,
        pchType: null,
        operUser: null,
        pchNum: null,
        pchPice: null,
        pchRemark: null,
        updateUser: null,
        createUser: null,
        time: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getClassList()//获取分类--子分类二位数组
  },
  methods: {
    /** 查询商品进货单列表 */
    getList() {
      this.loading = true;
      listPch(this.queryParams).then(response => {
        this.pchList = response.rows;
        this.total = response.total;
        this.assMoney = response.assMoney;
        this.assNum = parseInt(response.assNum);
        this.loading = false;
      });
    },
    //获取分类--子分类二位数组
    getClassList() {
      getClassList().then(res => {
        if (res.code == 200) {
          this.classList = res.data
        }
      })
    },
    //选择分类-筛选子分类
    changeClass(val) {
      this.subClassList = []
      this.$set(this.queryParams, 'sedCate', '')
      this.classList.forEach(item => {
        if (item.id == val) {
          this.subClassList = item.list
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //手动入库
    inbound() {
      this.$router.push({ path: '/stock/inbound', query: {} })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        prodNo: null,
        pchNo: null,
        pchType: null,
        operUser: null,
        pchNum: null,
        pchPice: null,
        pchRemark: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.beginTime = this.queryParams.time[0]
      this.queryParams.endTime = this.queryParams.time[1]
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品进货单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品进货单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品进货单编号为"' + ids + '"的数据项？').then(function () {
        return delPch(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/pch/export', {
        ...this.queryParams
      }, `pch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.in-detail {
  background-color: #f9f9f9;
  border-radius: 8px;
  border: 1px solid #ecebed;
  padding: 10px;
  display: flex;
  flex-wrap: wrap;

  div {
    display: inline-block;
    width: calc(33.3% - 20px);
    padding: 8px 10px;
    line-height: 24px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>